package com.jp;
public class LinkedList <E> extends AbstractList<E> {

    private Node<E>first;
    private Node<E>last;

    private static class Node<E> {
        E element;
        Node<E> prev;
        Node<E> next;
        public Node(E element, Node<E>prev, Node<E>next) {
            this.element = element;
            this.next = next;
            this.prev = prev;
        }
        @Override
        public String toString() {
            StringBuilder string = new StringBuilder();
            if (prev != null) {
				string.append(prev.element);
			} else {
				string.append("null");
			}
            string.append("_").append(element).append("_");
            if (next != null) {
				string.append(next.element);
			} else {
				string.append("null");
			}

            return string.toString();
        }
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
        last = null;
    }

    @Override
    public E get(int index) {
        return node(index).element;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = node(index);
        E oldElement = node.element;
        node.element = element;
        return oldElement;
    }

    @Override
    public void add(int index, E element) {
        
        rangeCheckForAdd(index);
        if (index == size) {
            Node<E> oldLast = last;
            last = new Node<E>(element, oldLast, null);
            if (oldLast == null) {
                first = last;
            } else {
                oldLast.next = last;
            }
        } else {
            Node<E> next = node(index);
            Node<E> prev = next.prev;
            Node<E> node = new Node<E>(element, prev, next);
            next.prev = node;
            if (prev == null) {
                first = node;
            } else {
                prev.next = node;
            }
        }
        size++;

    }

    @Override
    public E remove(int index) {

        rangeCheck(index);
        
        Node<E> node = node(index);
        Node<E> prev = node.prev;
        Node<E> next = node.next;
        
        if (prev == null) { // 0 
            first = next;
        } else {
            prev.next = node.next;
        }
        if (next == null) { // size - 1 
            last = prev;
        } else {
            next.prev = prev;

        }
        size--;
        return node.element;
    }

    @Override
    public int indexOf(E element) {
        if (element == null) {
            Node<E>node = first;
            for (int i = 0; i < size; i++) {
                if (node.element == null) return i;
                node = node.next;
            }
        } else {
            Node<E>node = first;
            for (int i = 0; i < size; i++) {
                if (node.element.equals(element)) return i;
                node = node.next;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    private Node<E> node(int index) {
        rangeCheck(index);
        Node<E> node = first;
        if (index < (size>>1)) {
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
        } else {
            node = last;
            for (int i = size-1; i > index; i--) {
                node = node.prev;
            }
        }
        return node;
    }
    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("size :").append(size).append(",[");
        Node<E> node = first;
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(", ");
            }
            string.append(node);
            node = node.next;
        }
        string.append("]");

        return string.toString();
    }

}